package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.Customer;
import com.example.management.entity.Customer;
import com.example.management.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    //查询
    @GetMapping("/list")
    public List<Customer> list() { return customerService.list(); }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Customer customer) {
        return customerService.save(customer) ? Result.success() : Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Customer customer) {
        return customerService.updateById(customer);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Customer customer) {
        return customerService.updateById(customer) ? Result.success() : Result.fail();
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOfMod(@RequestBody Customer customer) {
        return customerService.saveOrUpdate(customer);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(String cno) {
        return customerService.removeById(cno) ? Result.success() : Result.fail();
    }
    @GetMapping("/findByNo")
    public Result findByNo(String cno) {
        List<Customer> list = customerService.lambdaQuery().eq(Customer::getCno, cno).list();
        return list.isEmpty() ? Result.fail() : Result.success(list);
    }
    @GetMapping("/findByPhone")
    public Result findByPhone(String cphone) {
        List<Customer> list = customerService.lambdaQuery().eq(Customer::getCphone, cphone).list();
        return list.isEmpty() ? Result.fail() : Result.success(list);
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public List<Customer> listP(@RequestBody Customer customer) {
        LambdaQueryWrapper<Customer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Customer::getCname, customer.getCname());
        return customerService.list(lambdaQueryWrapper);
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String no = (String) param.get("cno");
        String name = (String) param.get("cname");
        Page<Customer> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Customer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(no) && !"null".equals(no)) {
            lambdaQueryWrapper.eq(Customer::getCno, no);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Customer::getCname, name);
        }

        IPage<Customer> data = customerService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}